package com.inspur.edp.commonmodel.engine.api.common;

import com.inspur.edp.lcm.metadata.api.IMetadataContent;
import com.inspur.edp.lcm.metadata.api.entity.GspMetadata;
import com.inspur.edp.lcm.metadata.api.entity.MetadataCustomizationFilter;
import com.inspur.edp.lcm.metadata.api.service.MetadataRTService;
import io.iec.edp.caf.commons.utils.SpringBeanUtils;

public class CMEngineUtil {

  private static volatile MetadataRTService mdSvr;

  public static MetadataRTService getMetadataRTSvr() {
    if (mdSvr == null) {
      mdSvr = SpringBeanUtils.getBean(MetadataRTService.class);
    }
    return mdSvr;
  }

  public static GspMetadata getMetadata(String id) {
    MetadataCustomizationFilter filter = new MetadataCustomizationFilter();
    filter.setMetadataId(id);
    filter.setIsI18n(false);
    return getMetadataRTSvr().getMetadata(filter);
  }

  public static <T extends IMetadataContent> T getMetadataContent(String id) {
    try {
      GspMetadata meta = getMetadata(id);
      return meta == null ? null : (T) meta.getContent();
    } catch (Throwable t) {
      throw new RuntimeException("获取元数据失败,元数据Id:" + id, t);
    }
  }
}
